package com.zzyl.serve.controller;

import java.util.List;
import java.util.Map;

import com.huaweicloud.sdk.iotda.v5.model.ServiceCapability;
import com.zzyl.serve.dto.DeviceDto;
import com.zzyl.serve.vo.DeviceDetailVo;
import com.zzyl.serve.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.serve.domain.Device;
import com.zzyl.serve.service.IDeviceService;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备信息Controller
 * 
 * @author ruoyi
 * @date 2025-05-10
 */
@Slf4j
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备信息相关接口")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备信息列表")
    public TableDataInfo<List<Device>> list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 同步列表功能
     * @return
     */
    @PostMapping("/syncProductList")
    @ApiOperation(value = "从物联网平台同步产品列表")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 1.2 查询所有产品列表
     */
    @GetMapping("/allProduct")
    public AjaxResult getAllProduct() {
        List<ProductVo> list = deviceService.getAllProduct();
        return AjaxResult.success(list);
    }
    /**
     * 1.3 注册设备
     * 接口地址:/nursing/device/register
     * 请求方式:POST
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody DeviceDto deviceDto) {
        deviceService.register(deviceDto);
        return AjaxResult.success();
    }

    /**
     * 1.5 查询设备详细数据
     * 接口地址:/nursing/device/{iotId}
     * 请求方式:GET
     */
    @GetMapping("/{iotId}")
    public AjaxResult getDeviceById(@PathVariable String iotId) {
        DeviceDetailVo vo = deviceService.getDeviceById(iotId);
        return AjaxResult.success(vo);
    }

    /**
     * 1.6 查看设备上报的数据
     * 接口地址: /nursing/device/queryServiceProperties/{iotId}
     * 请求方式:GET
     */

    @GetMapping("/queryServiceProperties/{iotId}")
    public AjaxResult queryServicePropertiesById(@PathVariable String iotId) {
        List<Map<String, Object>> list = deviceService.queryServicePropertiesById(iotId);
        return AjaxResult.success(list);
    }
    /**
     * 1.7 修改设备
     * 接口地址:/nursing/device
     * 请求方式:PUT
     */
    @PutMapping()
    public AjaxResult updateDevice(@RequestBody Device device) {
        log.info("{}" , device);
        deviceService.updateDeviceInfo(device);
        return AjaxResult.success();
    }

    /**
     * 1.8 删除设备
     * 接口地址:/nursing/device/{iotId}
     * 请求方式:DELETE
     * 请求示例:
     */
    @DeleteMapping("/{iotId}")
    public AjaxResult deleteDevice(@PathVariable String iotId) {
        deviceService.deleteDevice(iotId);
        return AjaxResult.success();
    }
    /**
     * 1.9 查询产品详情(报警规则模块使用)
     * 接口地址:/nursing/device/queryProduct/{productKey}
     * 请求方式:GET
     * 请求示例:
     */
    @ApiOperation("查询产品详情(报警规则模块使用)")
    @GetMapping("/queryProduct/{productKey}")
    public AjaxResult queryProduct(@PathVariable String productKey) {
        List<ServiceCapability> list = deviceService.queryProduct(productKey);
        return AjaxResult.success(list);
    }
}
